package com.qingcloud.adminbackend.base;

import com.qingcloud.adminbackend.constant.WamingConstant;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

@Data
@AllArgsConstructor
public class AjaxResult<T> implements Serializable {

    public AjaxResult() {
    }

    public AjaxResult(int code, String message) {
        this.erroCode = code;
        this.erroMsg = message;
    }

    /**
     * 成功处理方法
     *
     * @param data 返回数据
     * @param <T>  返回数据
     * @return AjaxResult
     */
    public static <T> AjaxResult<T> ok(T data) {
        AjaxResult<T> r = new AjaxResult<>();
        r.setErroCode(WamingConstant.SUCCESS);
        r.setResult(data);
        return r;
    }

    /**
     * 成功处理方法
     *
     * @param <T> 返回数据
     * @return AjaxResult
     */
    public static <T> AjaxResult<T> ok() {
        AjaxResult<T> r = new AjaxResult<>();
        r.setErroCode(WamingConstant.SUCCESS);
        r.setErroMsg("成功");
        return r;
    }

    public static <T> AjaxResult<T> success() {
        AjaxResult<T> r = new AjaxResult<>();
        r.setErroCode(WamingConstant.SUCCESS);
        r.setErroMsg("成功");
        return r;
    }

    public static AjaxResult fail(String message) {
        AjaxResult result = new AjaxResult();
        result.setErroCode(-1);
        result.setErroMsg(message);
        return result;
    }

    @ApiModelProperty(value = "返回码 2000正常 其他错误  6000未登录", example = "2000")
    protected int erroCode;

    @ApiModelProperty(value = "异常信息 2000正常 其他错误", example = "网络错误")
    protected String erroMsg;

    @ApiModelProperty(value = "AJAX结果数据", example = "网络错误")
    private T result;

}
